package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.BookFood;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class BookFoodDAO extends BaseDAO<BookFood>{

	public long addBookFood(BookFood food) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_book_food(");
		sb.append("foodName,pic,sellerId,menuId,foodId,userId,price,rebate,initMonthSales,type,praiseBaseCount,praiseCount,commentBaseCount,commentCount,images");
		sb.append(") values (");
		sb.append(":foodName,:pic,:sellerId,:menuId,:foodId,:userId,:price,:rebate,:initMonthSales,:type,:praiseBaseCount,:praiseCount,:commentBaseCount,:commentCount,:images");
		sb.append(")");
		
		update(sb.toString(), food);
		
		return getIncreamentId();
	}
	
	public void updateBookFood(BookFood food) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_book_food set ");
		sb.append(" foodName=:foodName,pic=:pic,sellerId=:sellerId,menuId=:menuId,price=:price,");
		sb.append(" rebate=:rebate,initMonthSales=:initMonthSales,images=:images,");
		sb.append(" praiseBaseCount=:praiseBaseCount,praiseCount=:praiseCount,commentBaseCount=:commentBaseCount,commentCount=:commentCount");
		sb.append(" where foodId = :foodId");
		
		update(sb.toString(), food);
	}
	
	public List<BookFood> listAllInfos(int type) throws Exception{
		String sql = "select * from him_book_food where type = ?";
		return getList(sql, BookFood.class,type);
	}
	
	public PageModel getPagedBookFoods(PageModel model,long sellerId,int menuId,String search,long userId,int type) throws Exception{
		StringBuilder querySQL = new StringBuilder(" select * from (select a.*,b.menuName,c.sellerName from him_book_food a,him_book_menu b,him_book_seller c where a.menuId = b.menuId and a.sellerId = c.sellerId) m ");
		StringBuilder countSQL = new StringBuilder(" select count(*) from (select a.*,b.menuName,c.sellerName from him_book_food a,him_book_menu b,him_book_seller c where a.menuId = b.menuId and a.sellerId = c.sellerId) m");
		
		StringBuilder whereSQL = new StringBuilder(" where foodName like ? and type = ?");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add("%"+search+"%");
		paramList.add(type);
		
		if(sellerId > 0){
			whereSQL.append(" and sellerId = ? ");
			paramList.add(sellerId);
		}
		
		if(sellerId == 0 && type == Constant.TYPE_BOOK_TICKET){
			querySQL = new StringBuilder(" select * from (select a.*,b.menuName from him_book_food a,him_book_menu b where a.menuId = b.menuId) m ");
			countSQL = new StringBuilder(" select count(*) from (select a.*,b.menuName from him_book_food a,him_book_menu b where a.menuId = b.menuId) m");
		}
		
		if(menuId > 0){
			whereSQL.append(" and menuId = ? ");
			paramList.add(menuId);
		}
		
		if(userId > 0){
			whereSQL.append(" and userId = ? ");
			paramList.add(userId);
		}
		
//		System.out.println(querySQL.toString()+whereSQL.toString());
//		System.out.println(paramList);
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, BookFood.class);
	}
	
	public BookFood getById(long foodId) throws Exception{
		String sql = "select * from him_book_food where foodId = ?";
		return this.getJavaBean(sql, BookFood.class, foodId);
	}
	
	public void delBookFood(long foodId) throws Exception{
		String sql = "delete from him_book_food where foodId = ?";
		this.commonUpdate(sql, foodId);
	}
	
	public long getPraiseCount(long foodId) throws Exception{
		String sql = "select praiseCount from him_book_food where foodId = ?";
		return this.getJdbcTemplate().queryForLong(sql,foodId);
	}
	
	public long getPraiseBaseCount(long foodId) throws Exception{
		String sql = "select praiseBaseCount from him_book_food where foodId = ?";
		return this.getJdbcTemplate().queryForLong(sql,foodId);
	}
	
	public long getCommentCount(long foodId) throws Exception{
		String sql = "select commentCount from him_book_food where foodId = ?";
		return this.getJdbcTemplate().queryForLong(sql,foodId);
	}
	
	public long getCommentBaseCount(long foodId) throws Exception{
		String sql = "select commentBaseCount from him_book_food where foodId = ?";
		return this.getJdbcTemplate().queryForLong(sql,foodId);
	}
	
	public void addPraiseCount(long infoId) throws Exception {
		String sql = "update him_book_food set praiseCount = praiseCount + 1 where foodId = ?";
		this.commonUpdate(sql, infoId);
	}
	
	public void addCommentCount(long infoId) throws Exception {
		String sql = "update him_book_food set commentCount = commentCount + 1 where foodId = ?";
		this.commonUpdate(sql, infoId);
	}
	
	/**
	 * 获取收藏信息
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public PageModel getCollegeInfos(long userId,int moduleType,PageModel model) throws Exception{
		String sql = "select a.type,b.* from him_user_collection a,him_book_food b where a.infoId = b.foodId";
		StringBuilder querySQL = new StringBuilder("select * from ("+sql+") as m");
		StringBuilder countSQL = new StringBuilder("select count(*) from ("+sql+") as m");
		StringBuilder whereSQL = new StringBuilder(" where userId = ? and type = ?");
		
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(userId);
		paramList.add(moduleType);
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, BookFood.class);
	}
}
